package com.gome.extract;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map.Entry;
import java.util.ResourceBundle;
import java.util.Vector;

import org.apache.log4j.Logger;

import com.gome.extract.common.AppConfiguer;
import com.gome.extract.common.GlobalUtils;
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;

public class FileReader extends GlobalUtils
{
//	private static final String ENCODE = "UTF8";
//    private static final String KSEP = "\t";
//    private static final String SEP = "@@";
//    private StaticHash<String> mDict = new StaticHash<String>();
	private static Logger logger = Logger.getLogger(FileReader.class);
	
    private DBOperator mDBOp = null;
    private HashDict mDict = null;
    private ResourceBundle mConf = null;
    private String mDictName = "";
    private String mDataName = "";
	public FileReader(String dictName, String dataName)
	{
		mDBOp = new DBOperator();
		mDictName = dictName;
		mDataName = dataName;
		loadDict(dictName);
	}
    
	public void loadDict(String dictName)
	{
		mDict = new HashDict(dictName);
		mDict.load();
	}
	
	
	public Vector<Integer> getIDList(String[] vec)
	{
		Vector<Integer> v = new Vector<Integer>();
		for(int i=0; i<vec.length; ++i)
		{
			if(vec[i]==null || vec[i].length()==0)
					continue;
			 String s = vec[i].toLowerCase();
			 int id = mDict.get(s);
	   		 if(id!=-1)
	   		 {
	   			 v.add(id);
	   		 }
		}
		return v;
	}
	
	public HashMap<Integer, String> getMapList(Vector<String> vec)
	{
		HashMap<Integer, String> mv = new HashMap<Integer, String>();
		for(int i=0; i<vec.size(); ++i)
		{
			 if(vec.elementAt(i)==null || vec.elementAt(i).length()==0)
				continue;
			 String s = vec.elementAt(i).toLowerCase();
			 String [] sa = s.split(TAB);
			 String k = sa[0];
			 String v = sa[1];
			 int id = mDict.get(v);
	   		 if(id!=-1)
	   		 {
	   			 mv.put(id, k);
	   		 }
		}
		return mv;
	}

	public void run(String stepID, String nameValue, String insertCollectName)
	{ 
		String dbName = AppConfiguer.getStringByKey("read_db");
		String confCollectName = AppConfiguer.getStringByKey("conf_collect");
		String resultDB =  AppConfiguer.getStringByKey("insert_db");
		
		Vector<String> vec = mDBOp.read(dbName, confCollectName, stepID, nameValue); 
		HashMap<Integer, String> mv = getMapList(vec);
		int nSize = mv.size();		
		try
    	{
    		 BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream( new File(mDataName)), ENCODE) );
             String strContent="";
             String line;
             boolean b = false;
             Vector<DBObject> vObj = new Vector<DBObject>();
             while ( (line = reader.readLine()) != null)
             {
            	 DBObject obj = new BasicDBObject();
            	 String[] array1 = line.split(TAB);
            	 if(array1.length!=4 || array1[0].equals(stepID)==false)
            		 continue;
            	 String[] array2 = array1[3].split(SEP);
            	 obj.put("id", array1[1]);
            	 obj.put("pid", array1[2]);
            	 obj.put("stepID", stepID);
            	 for(Entry<Integer, String> entry : mv.entrySet())
            	 {
            		 int id = entry.getKey();
            		 String v = entry.getValue().split(CON)[0];
            		 
            		 if(id<array2.length && array2[id].length()!=0)
            		 {
            			 obj.put(v, array2[id]);
            			 b = true;
            		 }
            	 }
            	 if(b)
            	 {
            		 b = false;
            		 vObj.add(obj); 
            	 }
             }

             if(insertCollectName!=null && insertCollectName.length()!=0)
             {
            	 resultDB = insertCollectName;
             }
             logger.info(dbName + "\t" + confCollectName + "\tinsert_table:\t" + resultDB);            
             reader.close();
             mDBOp.insert(resultDB, nameValue, vObj);
    	}
    	catch(Exception e)
    	{
    		logger.error(AppConfiguer.getTrace(e));
    	}
	}
}










